﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KardexSist.UserControls.Common;
using Kardex_Entity;
using Kardex_BussinesLogic;
using Kardex_Common;
using KardexSist.Library;

namespace KardexSist.UserControls.PreSale
{
    public partial class CtrlPreSale : UserControl, IOperations
    {
        private person PersonCurrent;
        private  PreventistaFactory  PreventistaFc;

        #region LanzarEventos
        public delegate void MyDelegadoBuscarEventHandler();
        public event MyDelegadoBuscarEventHandler MostrarListaClick;

        protected virtual void OnMostrarListaClick()
        {
            if (MostrarListaClick != null)
            { MostrarListaClick(); }
        }
        #endregion              

        public CtrlPreSale()
        {
            InitializeComponent();
            PersonCurrent = new person() { preventista = new preventista() }; 
        }
        private void CtrlPreSale_Load(object sender, EventArgs e)
        {
            OnMostrarListaClick();
        }

        public void TrigerObjPerson(person objPerson)
        {
            PersonCurrent = new person() { preventista = new preventista() };
            PersonCurrent = objPerson;
            LoadObjectPeson();
        }
        
        private void LoadObjectPeson()
        {
            try
            {
                txtCd.Text = PersonCurrent.IdCD;
                txtCi.Text = PersonCurrent.CI;
                txtName.Text = PersonCurrent.Name;
                txtPhone.Text = PersonCurrent.Telephone;
                cmbCity.SelectedItem = PersonCurrent.IdTipoCIExpedido;
                txtCodigoHH.Text = PersonCurrent.preventista.CodigoHH;
                if (PersonCurrent.Activ == true)
                {
                    chkActive.Checked = true;
                }
                else
                {
                    chkActive.Checked = false;
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }
       
        private void LoadData()
        {
            PersonCurrent.IdCD = txtCd.Text;
            PersonCurrent.CI = txtCi.Text;
            PersonCurrent.IdTipoCIExpedido = cmbCity.Text;
            PersonCurrent.Name = txtName.Text;            
            PersonCurrent.Telephone = txtPhone.Text;
            PersonCurrent.Activ = chkActive.Checked;
            PersonCurrent.preventista.CodigoHH = txtCodigoHH.Text;
        }

        private void ClearData()
        {
            //_numericIdPersona.Value = 0;
            txtCd.Text = string.Empty;
            txtCi.Text = string.Empty;
            txtName.Text = string.Empty;            
            txtPhone.Text = string.Empty;
            chkActive.Checked = false;
            txtCodigoHH.Text = string.Empty;
        }

        public void Add()
        {
            ClearData();
            PersonCurrent = new person() { preventista = new preventista() };
        }

        public void Delete()
        {
            try
            {
                var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionDelete, null);
                if (result == DialogResult.Yes)
                {

                    PreventistaFc = new PreventistaFactory();
                    if (PreventistaFc.Delete(PersonCurrent) == true)
                    {
                        OnMostrarListaClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Delete, null);
                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Save()
        {
            try
            {
                //insert
                if (PersonCurrent.IdPerson <= 0)
                {
                    PersonCurrent = new person() { preventista = new preventista() };
                    LoadData();
                    PreventistaFc = new PreventistaFactory();
                    PersonCurrent = PreventistaFc.Save(PersonCurrent);
                   // _numericIdPersona.Value = PersonCurrent.IdPerson;
                    if (PersonCurrent.IdPerson > 0)
                    {
                        OnMostrarListaClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Insert, null);
                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
                else
                {
                    var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionModif, null);
                    if (result == DialogResult.Yes)
                    {

                        LoadData();
                        PreventistaFc = new PreventistaFactory();
                        if (PreventistaFc.Update(PersonCurrent) == true)
                        {
                            OnMostrarListaClick();
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Update, null);
                        }
                        else
                        {
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                        }
                    }
                    else
                    {                        
                        ClearData();
                        LoadObjectPeson();
                    }
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Export()
        {
           
        }
    }
}
